<style>
  .demo-upload-list{
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,.2);
    margin-right: 4px;
  }
  .demo-upload-list img{
    width: 100%;
    height: 100%;
  }
  .demo-upload-list-cover{
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.6);
  }
  .demo-upload-list:hover .demo-upload-list-cover{
    display: block;
  }
  .demo-upload-list-cover i{
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }
  .img{
    width: 100px;
    height: 120px;
  }
</style>
<template>
  <div>
    <div class="demo-upload-list" v-for="item in uploadList" :key="item.id">
      <template v-if="item.status === 'finished'">
        <img :src="item.url ? item.url : row.host+row.img">
        <div class="demo-upload-list-cover">
          <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
        </div>
      </template>
    </div>
    <Upload
      v-show="uploadList.length===0"
      ref="upload"
      :show-upload-list="false"
      :default-file-list="defaultList"
      :on-success="handleSuccess"
      :format="['jpg','jpeg','png']"
      :max-size="2048"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      :headers="{
        'agent': 'Web',
        'token': token,
      }"
      type="drag"
      :action="actionUrl"
      style="display: inline-block;width:58px;">
      <div style="width: 58px;height:58px;line-height: 58px;">
        <Icon v-show="!row.value" type="ios-camera" size="20"></Icon>
        <img v-if="row.value" style="width: 58px;height:58px;line-height: 58px;" :src="row.value ? row.host+row.value : ''" alt="">
      </div>
    </Upload>
  </div>
</template>
<script>
import {
  mapState,
  mapActions,
  mapMutations
} from 'vuex'
import { getToken } from '../../libs/util'
export default {
  name: 'UploadAppImg',
  props: {
    folder: '',
    row: null
  },
  data () {
    return {
      url: '',
      host: '',
      actionUrl: '',
      loadingStatus: false,
      defaultList: [],
      uploadList: [],
      imgName: '',
      token: getToken(),
      form: {
        ctime: '',
        activitytime: '',
        type: 'all',
        method: 'all',
        state: 'all'
      }
    }
  },
  computed: {
    rules () {
      return {
        ctime: this.ctimeRules,
        activitytime: this.activitytimeRules,
        type: this.typeRules,
        method: this.methodRules,
        state: this.stateRules
      }
    }
  },
  methods: {
    handleView (name) {
      this.imgName = name
      this.visible = true
    },
    handleRemove (file) {
      const fileList = this.$refs.upload.fileList
      this.$refs.upload.fileList.splice(fileList.indexOf(file), 1)
    },
    handleSuccess (response, file, fileList) {
      file.url = response.host + response.data
      this.host = response.host
      this.url = response.data
      this.$emit('upload-success', this.url)
    },
    handleFormatError (file) {
      this.$Notice.warning({
        title: '文件格式不正确',
        desc: '请上传jpg、jpeg、png格式的图片'
      })
    },
    handleMaxSize (file) {
      this.$Notice.warning({
        title: '文件大小不正确',
        desc: '请上传2M以下大小的图片'
      })
    },
    handleBeforeUpload (file) {
      this.fileName = file.name
      const check = this.uploadList.length < 1
      if (!check) {
        this.$Notice.warning({
          title: '只能上传1张图片'
        })
      }
      return check
    }
  },
  mounted () {
    this.uploadList = this.$refs.upload.fileList
    this.actionUrl = process.env.NODE_ENV === 'development' ? `/api/upload?folder=${this.folder}` : `http://rbackapi.baisha.work/upload?folder=${this.folder}`
  }
}
</script>
